/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaclientcp3110.View.Product;

import common.Objects.Category;
import common.Objects.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Vector;
import javaclientcp3110.Model.MainModel;
import javaclientcp3110.View.Component.ComponentListView;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author Slasher
 */
public class ComponentsListedViaCatagorysView extends JPanel {
    private Vector<ComponentListView> componentView;
    public ComponentsListedViaCatagorysView() {
        componentView = new Vector<ComponentListView>();        
    }
    public void createList () {
        componentView.removeAllElements();
        this.removeAll();
        MainModel.getInstance().getProducts();
        setLayout(new GridLayout(0,1));
        JPanel tempContainer = new JPanel();
        tempContainer.setLayout(new GridLayout(0,1));
        
        for (Category category : MainModel.getInstance().getCategorys()) {
            JPanel temp = new JPanel(new GridLayout(0,1));
            componentView.add(new ComponentListView());
            componentView.lastElement().populateComboBox_viaCategory(category.getId());

            temp.add(new JLabel(category.getName()+":"));
            temp.add(componentView.lastElement());
            tempContainer.add(temp);
        }
        
        JScrollPane sp = new JScrollPane(tempContainer);
        sp.setBorder(null);
        sp.setPreferredSize(new Dimension(220,150));
        this.add(sp);
        repaint();
    }
    public void createList(Vector<Component> components) {        
        componentView.removeAllElements();
        this.removeAll();
        if (components != null) {
            MainModel.getInstance().getProducts();
            setLayout(new GridLayout(0,1));
            JPanel tempContainer = new JPanel();
            tempContainer.setLayout(new GridLayout(0,1));

            for (Category category : MainModel.getInstance().getCategorys()) {
                JPanel temp = new JPanel(new GridLayout(0,1));
                componentView.add(new ComponentListView());
                componentView.lastElement().populateComboBox_viaCategory(category.getId());
                for (Component c : componentView.lastElement().getObjects_Components()) {
                    for (Component inputComp : components) {
                        if (c.getId() == inputComp.getId()) {
                            componentView.lastElement().setSelectedItem(c);
                        } else {
                            
                        }
                    }
                }                
                temp.add(new JLabel(category.getName()+":"));
                temp.add(componentView.lastElement());
                tempContainer.add(temp);
            }
            
            JScrollPane sp = new JScrollPane(tempContainer);
            sp.setBorder(null);
            sp.setPreferredSize(new Dimension(220,150));
            this.add(sp);
            validate();
        }
    }
    public Vector<ComponentListView> getComponentView() {
        return componentView;
    }
}
